export const randomPos = (element: any) => {
    const ulElements = document.querySelectorAll(element)
    ulElements.forEach(ulElement => {
        const liElements = ulElement.getElementsByTagName('li')
        for (let i = 0; i < liElements.length; i++) {
            let randomValue: any = Math.floor(Math.random() * 60)
            if (i == liElements.length - 1) {
                randomValue = Math.floor(Math.random() * -5)
            }
            liElements[i].style.setProperty('--star-top', randomValue)
        }
    })
}
export const fixedTop = () => {
    window.addEventListener('scroll', function (e) {
        let t = document.documentElement.scrollTop || document.body.scrollTop
        if (t < 80) {
            document.body.classList.remove('nav-fixed')
        } else {
            document.body.classList.add('nav-fixed')
        }
    })
}

export const timeAgo = (dateTimeStamp: any) => {
    let minute: number = 1000 * 60
    let hour: number = minute * 60
    let day: number = hour * 24
    let week: number = day * 7
    let month: number = day * 30
    let now: number = new Date().getTime()
    let diffValue: number = now - dateTimeStamp
    if (diffValue < 0) {
        return
    }
    let minC: number = diffValue / minute
    let hourC: number = diffValue / hour
    let dayC: number = diffValue / day
    let weekC: number = diffValue / week
    let monthC: number = diffValue / month
    let result
    if (monthC >= 1) {
        result = '' + parseInt(String(monthC)) + '月前'
    } else if (weekC >= 1) {
        result = '' + parseInt(String(weekC)) + '周前'
    } else if (dayC >= 1) {
        result = '' + parseInt(String(dayC)) + '天前'
    } else if (hourC >= 1) {
        result = '' + parseInt(String(hourC)) + '小时前'
    } else if (minC >= 1) {
        result = '' + parseInt(String(minC)) + '分钟前'
    } else {
        result = '刚刚'
    }
    return result
}

export const timeFormat = (time: string): string => {
    return time.split(' ')[0]
}

export const articleTimeFormat = (time: string): string => {
    return time.split('T')[0]
}
export const timeInfoComputed = (time: any, inc: number) => {
    let year = new Date(time).getFullYear()
    let month = new Date(time).getMonth() + 1
    let day = new Date(time).getDate()
    return (year + inc) + '年' + month + '月' + day + '日'
}

export const timeDifference = (nowTime: any) => {
    let timeOld = new Date().getTime() - new Date(nowTime).getTime()
    let msPerDay = 24 * 60 * 60 * 1000
    return Math.floor(timeOld / msPerDay)
}

export const handleErrorFriendAvatar = (event: Event, name: string, background: string) => {
    const imgError = event.target as HTMLImageElement
    imgError.src = 'https://ui-avatars.com/api/?rounded=true&background=' + background + '&color=fff&name=' + name
}

export const deleteHTMLTag = (content: string) => {
    return content
        .replace(/<\/?[^>]*>/g, '')
        .replace(/[|]*\n/, '')
        .replace(/&npsp;/gi, '')
        .replace(/#/g, '')
}


export const getDayOfWeek = (dateString: string) => {
    const daysOfWeek = ['周日', '周一', '周二', '周三', '周四', '周五', '周六'];
    const date = new Date(dateString);
    const dayIndex = date.getDay();
    return daysOfWeek[dayIndex];
}

export const handleErrorArticleImage = (event: Event) => {
    const imgError = event.target as HTMLImageElement
    imgError.src = '/static/img/gd.jpg'
}

export const copyInfo = (copyText: any) => {
    navigator.clipboard.writeText(copyText).then(() => {
        ElMessage({
            message: '链接复制成功🎉',
            type: 'success',
            center: true
        })
    })
}

export const addScrollEventPrev = (selector: any, li: any) => {
    const cl = document.querySelector(selector)
    const ul = cl.querySelector('.no-scrollbar')
    const liWidth = ul.querySelector(li).offsetWidth
    ul.scrollBy({left: -liWidth, behavior: 'smooth'})
}

export const addScrollEventNext = (selector: any, li: any) => {
    const cl = document.querySelector(selector)
    const ul = cl.querySelector('.no-scrollbar')
    const liWidth = ul.querySelector(li).offsetWidth
    ul.scrollBy({left: liWidth, behavior: 'smooth'})
}

let refreshCaptchaInterval: any = null;

export function beginRefreshCaptchaInterval(expireSeconds: any, getCaptcha: Function) {
    if (refreshCaptchaInterval === null) {
        refreshCaptchaInterval = setInterval(getCaptcha, (expireSeconds - 5) * 1000);
    }
}
